package com.mxl.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;

/**
 * @author MXL
 * @date 2021/9/14 0014
 **/
//@Configuration
public class TopicConfig {

    public final static String QUEUE_NAME_TOPIC_XIAOMI = "mxl.topic.xiaomi";
    public final static String QUEUE_NAME_TOPIC_HUAWEI = "mxl.topic.huawei";
    public final static String QUEUE_NAME_TOPIC_PHONE = "mxl.topic.phone";
    public final static String EXCHANGE_NAME_TOPIC = "mxl.topic";

    @Bean
    public Queue xiaomiQueue() {
        return new Queue(QUEUE_NAME_TOPIC_XIAOMI);
    }

    @Bean
    public Queue huaweiQueue() {
        return new Queue(QUEUE_NAME_TOPIC_HUAWEI);
    }

    @Bean
    public Queue phoneQueue() {
        return new Queue(QUEUE_NAME_TOPIC_PHONE);
    }

    @Bean
    TopicExchange topicExchange() {
        return new TopicExchange(EXCHANGE_NAME_TOPIC);
    }

    /**
     * 将firstQueue和topicExchange绑定,而且绑定的键值为topic.man
     * 这样只要是消息携带的路由键是topic.man,才会分发到该队列
     * @return
     */
    @Bean
    Binding bindingXiaomi() {
        return BindingBuilder.bind(xiaomiQueue()).to(topicExchange()).with("xiaomi.#");
    }

    /**
     * 将secondQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.#
     * 这样只要是消息携带的路由键是以topic.开头,都会分发到该队列
     * @return
     */
    @Bean
    Binding bindingHuawei() {
        return BindingBuilder.bind(huaweiQueue()).to(topicExchange()).with("huawei.#");
    }

    @Bean
    Binding bindingPhone() {
        return BindingBuilder.bind(phoneQueue()).to(topicExchange()).with("#.phone.#");
    }


}
